import storage from 'store';
var $ = require('jquery');
import { md5 } from '@/utils/md5';
import api from '@/api';

// 签名验签
export function dataEncryption(params){
    let time = ''
    time = new Date($.ajax({type:'HEAD',async: false,cache: false}).getResponseHeader("Date")).getTime();
    if(!time){
        time = new Date().getTime()
    }
    const signKey = storage.get('signKey');
    let obj = {
        'timeStamp':time,
    }
    let dataObj = Object.assign(params,obj)
    // let data = new Object()
    let arr = new Array()
    // console.log(dataObj)
    Object.keys(dataObj).sort().map(function(key){
        // 判断key值的类型，文件的过滤掉
        if(typeof(dataObj[key]) != 'object'  && dataObj[key] !== "" && dataObj[key] != undefined){
            arr.push(
            key + '=' + dataObj[key]
            )
        }
    })
    arr.push()
    let str = md5(arr.join('&') + `&signKey=${signKey}`).toUpperCase()
    let objData = {
        'sign':str,
        'timeStamp':time,
    }
    console.log(objData,'objData')
    return objData
}

// 密码数字+字母验证
export function validPsw(password,min = 6,max = 16) {  
    var num = 0;  
    var number = 0 ;  
    var letter = 0 ;  
    var bigLetter = 0 ;  
      
    if (password.search(/[0-9]/) != -1) {  
        num += 1;  
        number =1;  
    }  
    if (password.search(/[A-Z]/) != -1) {  
        num += 1;  
        bigLetter = 1 ;  
    }  
    if (password.search(/[a-z]/) != -1) {  
        num += 1;  
        letter = 1 ;  
    }  
    if(password.length < min || password.length > max){
        return false
    }
    if(number>=1&&(bigLetter+letter<=0)){  
        return false
    }  
    if(number==0&&(bigLetter+letter>=1)){
        return false
    }
    var reg = /^[0-9a-zA-Z]+$/;
    if(!reg.test(password)){
        return false
    }
    return true
} 

import { userAgent } from './types'
// 判断手机终端信息
export function getUserAgent(){
    var u = navigator.userAgent
    if(u.indexOf('MicroMessenger') != -1){
        return userAgent.WECHAT;
    }else if(!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)){
        return userAgent.IOS;
    }else if(u.indexOf('Android') > -1 || u.indexOf('Linux') > -1 || u.indexOf('Adr') > -1){
        return userAgent.ANDROID;
    }else{
        return userAgent.ANDROID;
    }
}

// 获取日期
export function getDate(day){
    if(!day){
        day = 0
    }
    const date = new Date()
    const newDate = new Date(date.getTime() - (day * 24 * 3600 * 1000));
    let newDateStr = newDate.getFullYear() + '-' + (newDate.getMonth() < 10 ? ( '0' + (newDate.getMonth() + 1)) : (newDate.getMonth() + 1)) + '-' + (newDate.getDate() < 10 ? ( '0' + (newDate.getDate())) : (newDate.getDate() ))
    return newDateStr
}

// 根据str获取手机运营商type
export function getOperator (operatorStr){
    var operator = '1'
    switch(operatorStr){
        case '联通':
            operator = '2';
            break;
        case '电信':
            operator = '3'
            break;
        default:
            break;
    }
    return operator;
}

// 每次请求去掉空值参数
export function delNoneParameter(data){
    let obj = new Object()
    Object.keys(data).sort().map(function(key){
        // 判断key值的类型，文件的过滤掉
        if(data[key] !== "" && data[key] != undefined && data[key] != null && data[key] != 'null'){
            obj[key] = data[key]
        }
    })
    return obj
}

// 根据title 获取后台新闻链接
export function getLinks(title){
    return new Promise((resolve,reject) => {
        api.brandApi.queryAppInforMtion({infoType : 2, title : title, page : 0, size : 20, isBrand: true})
        .then(res=>{
            if(res.success){
                resolve(res.data.content)
            }else{
                reject(res.errMessage)
            }
        })
    })
}